package com.smile.frame.api.demo.redis;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * redis 过期 key 监听
 *
 * @author LGC
 * @date 2021/11/12 14:57
 */
@Slf4j
@Component
public class KeyExpirationListener extends KeyExpirationEventMessageListener {
    //
    public KeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        Integer id = null;
        String key = message.toString();
        if (key.startsWith("reservation:")) {
            String[] split = key.split(":");
            if (split.length == 2) {
                id = Integer.valueOf(key.split(":")[1]);
            }
        }
        if (id != null) {
            log.info("监听Redis key过期，key：{}，channel：{}", id, new String(pattern));
//            DemoReservationInfoDO demoReservationInfoDO = demoReservationInfoDOMapper.selectById(id);
//            if (demoReservationInfoDO != null && demoReservationInfoDO.getStatus().equals(1)) {
//                new LambdaUpdateChainWrapper<>(demoReservationInfoDOMapper)
//                        .set(DemoReservationInfoDO::getStatus, 3)
//                        .eq(DemoReservationInfoDO::getId, id).update();
//                log.info("更新预约单：{},过期成功", id);
//            }
        }
    }
}